package com.gvtv.main.xml;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gvtv.main.job.JobInfo;
import com.gvtv.main.job.ParamInfo;
import com.gvtv.main.schedul.SchedulInfo;

@SuppressWarnings("unchecked")
public class SchedulJobXmlParaser {
	
	private Document document;
	private List<SchedulInfo> schedulList;
	private List<File> fileList;
	
	public SchedulJobXmlParaser(){
	}
	public SchedulJobXmlParaser(String path){
		this(new File(path));
	}
	public SchedulJobXmlParaser(File file){
		if(fileList==null) 
			fileList = new ArrayList<File>();
		fileList.add(file);
	}
	public SchedulJobXmlParaser(List<File> files){
		this.fileList = files;
	}
	public void addFile(String path){
		if(fileList==null) 
			fileList = new ArrayList<File>();
		fileList.add(new File(path));
	}
	
	public List<SchedulInfo> getSchedulList() {
		return schedulList;
	}
	public void setSchedulList(List<SchedulInfo> schedulList) {
		this.schedulList = schedulList;
	}
	public void addSchedulList(SchedulInfo schedulInfo) {
		if(this.schedulList == null){
			this.schedulList = new ArrayList<SchedulInfo>();
		}
		this.schedulList.add(schedulInfo);
	}
	public List<File> getFileList() {
		return fileList;
	}
	public void setFileList(List<File> fileList) {
		this.fileList = fileList;
	}
	
	public void paraser(){
		SAXReader reader = new SAXReader();
		if(fileList!=null && !fileList.isEmpty()){
			try {
				for (File file : fileList) {
					document = reader.read(file);
					Element root = document.getRootElement();
					List<Element> scheduls = root.elements("schedul");
					for (Element s : scheduls) {
						SchedulInfo schedulInfo = this.createSchedulInfo(s);
						this.createEnvs(s, schedulInfo);
						List<Element> jobs = s.elements("job");
						for (Element job : jobs) {
							JobInfo jobInfo = this.createJobInfo(job);
							List<Element> params = job.elements("param");
							for (Element p : params) {
								jobInfo.addParam(this.createParamInfo(p));
							}
							schedulInfo.addJobInfo(jobInfo);
						}
						this.addSchedulList(schedulInfo);
					}
				}
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
		this.clear();
	}
	public void clear(){
		this.document = null;
	}
	
	private void createEnvs(Element s, SchedulInfo schedulInfo){
		List<Element> envs = s.elements("env");
		if(envs!=null){
			for(Element env : envs){
				List<Element> params = env.elements("param");
				for (Element p : params) {
					Attribute attr = p.attribute("name");
					schedulInfo.addEnv(env.getName()+"."+attr.getData().toString(), p.getData().toString());
				}
			}
		}
	}
	
	private SchedulInfo createSchedulInfo(Element s){
		SchedulInfo schedulInfo = new SchedulInfo();
		List<Attribute> attributes = s.attributes();
		try {
			for (Attribute attr : attributes) {
				BeanUtils.setProperty(schedulInfo, attr.getName(), attr.getText());
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return schedulInfo;
	}
	
	private JobInfo createJobInfo(Element s){
		JobInfo jobInfo = new JobInfo();
		List<Attribute> attributes = s.attributes();
		try {
			for (Attribute attr : attributes) {
				BeanUtils.setProperty(jobInfo, attr.getName(), attr.getText());
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return jobInfo;
	}
	
	private ParamInfo createParamInfo(Element s){
		ParamInfo paramInfo = new ParamInfo();
		List<Attribute> attributes = s.attributes();
		try {
			for (Attribute attr : attributes) {
				BeanUtils.setProperty(paramInfo, attr.getName(), attr.getText());
			}
			if(StringUtils.isEmpty(paramInfo.getValue())){
				BeanUtils.setProperty(paramInfo, "value", s.getData());
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return paramInfo;
	}
	/*public static void main(String[] args) {
		XmlParaser paraser = new XmlParaser();
		//paraser.addFile("jobs/tvpad-job.xml");
		paraser.addFile("jobs/website-job.xml");
		//paraser.addFile("jobs/website-job.xml");
		//paraser.addFile("jobs/website-job.xml");
		paraser.paraser();
		List<SchedulInfo> list = paraser.getSchedulList();
		for (SchedulInfo schedulInfo : list) {
			System.out.println(schedulInfo);
			for (JobInfo jobInfo : schedulInfo.getJobs()) {
				//System.out.println(jobInfo);
				for(ParamInfo paramInfo : jobInfo.getParams()){
					//System.out.println(paramInfo);
				}
			}
		}
	}*/
}
